﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Bll;
using AspNetCs.Model;
using AspNetCs.Common;
using AspNetCs.Config;

public partial class Master_Admin_announce :Admin_BasePage
{
    string whereStr = "";
    Annouce annouceBLL = new Annouce();
    protected void Page_Load(object sender, EventArgs e)
    {
        IsExit();
        if(!IsPostBack)
        {
            if (Request["Action"] != null)
            {
                showPanel(Request["Action"]);
            }
            this.AnnouceWebEdit.UploadFolder =configInfo.InstallDir + configInfo.UpFolder+"Master_up/";
        }
        
    }

    protected void dataBind()
    {
        Pager p = new Pager();
        p.Obj = rptAnnouceList;
        p.TableName = "Announce";
        p.WhereStr = whereStr;
        p.Orderfld = "id";
        p.PageSize = this.AspNetPager1.PageSize;
        p.PageIndex = this.AspNetPager1.CurrentPageIndex;
        p.OrderType = "desc";
        p.ControlBind();

        this.AspNetPager1.RecordCount = p.recordcount();

    }

    //绑定公告列表
    //public void dataBind()
    //{
    //    rptAnnouceList.DataSource = annouceBLL.GetAnnounceList();
    //    rptAnnouceList.DataBind();
    //}

    //显示公告的详细信息
    protected void initAnnounce(int id)
    {
        Announce_Model model = null;
        model = annouceBLL.getAnnouceById(id);
        if(model!=null)
        {
            txtAnnounceTitle.Text = model.Title;
            AnnouceWebEdit.Text = model.Content;
            isHot.SelectedValue = model.Hot.ToString();
            isNew.SelectedValue = model.IsNew.ToString();
        }
        btnSubmit.Visible = false;
        btnUpdate.Visible = true;
    }

    /// <summary>
    /// 获得网站公告的Model
    /// </summary>
    /// <returns></returns>
    protected Announce_Model getAnnounceModel()
    {
        Announce_Model model = new Announce_Model();
        model.Id = Convert.ToInt32(ViewState["id"].ToString());
        model.Title = txtAnnounceTitle.Text;
        model.Content = AnnouceWebEdit.Text;
        model.Hot = Convert.ToInt32(isHot.SelectedValue);
        model.IsNew = Convert.ToInt32(isNew.SelectedValue);
        model.Addtime = DateTime.Now;
        return model;
    }


    //显示不同的Panel
    protected void showPanel(string requestType)
    { 
        if(requestType=="Add")
        {
            palAddAnnounce.Visible = true;
            palListAnnounce.Visible = false;
            btnSubmit.Visible = true;
            btnUpdate.Visible = false;
        }
        else if(requestType=="Update")
        {
            palAddAnnounce.Visible = true;
            palListAnnounce.Visible = false;
            btnSubmit.Visible = false;
            btnUpdate.Visible = true;
        }
        else if(requestType=="List")
        {
            palAddAnnounce.Visible = false;
            palListAnnounce.Visible = true;
            dataBind();
        }
    }



    //添加网站公告
    protected void AddAnnouce()
    {
        string title = "";
        string content = "";
        int hot = 0;
        int isNew = 0;
        bool isSuccee = true;

        title = txtAnnounceTitle.Text;
        content = AnnouceWebEdit.Text;
        hot =Convert.ToInt32(isHot.SelectedValue);
        isNew = Convert.ToInt32(this.isNew.SelectedValue);

        Announce_Model annouceModel = new Announce_Model();
        annouceModel.Title = title;
        annouceModel.Content = content;
        annouceModel.Hot = hot;
        annouceModel.IsNew = isNew;
        annouceModel.Addtime = DateTime.Now;
        isSuccee=annouceBLL.addAnnounce(annouceModel);

        palAddAnnounce.Visible = false;
        palListAnnounce.Visible = true;
        dataBind();
        
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        AddAnnouce();
    }
    protected void rptAnnouceList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        LinkButton lbtn = e.CommandSource as LinkButton;      
        try
        {
            string id = lbtn.CommandArgument;
            int announceId = Convert.ToInt32(id);
            if (e.CommandName == "Modify")
            {
                palAddAnnounce.Visible = true;
                palListAnnounce.Visible = false;
                ViewState["id"] = announceId;
                initAnnounce(announceId);
            }
            else if (e.CommandName == "delete")
            {
                bool bo=annouceBLL.deleteAnnounce(announceId);
                if (bo)
                {
                    showMessage("deleteSucce","删除成功");
                    dataBind();
                }
                else
                {
                    showMessage("deleteFaith", "删除失败");
                    dataBind();
                }
            }
        }
        catch(Exception ex)
        {
            new CustomException(ex);
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        bool bo = false;
        Announce_Model model = getAnnounceModel();
        if(model!=null)
        {
            bo=annouceBLL.updateAnnouce(model);
        }
        if (bo)
        {
            showMessage("updateSucce", "修改成功");

        }
        else
        {
            showMessage("updateFaith", "修改失败");
        }
        palAddAnnounce.Visible = false;
        palListAnnounce.Visible = true;
        dataBind();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        dataBind();
    }
}
